#include "stdafx.h"
#include "YProactorTokenOnSendTo.h"
#include "IUDPSession.h"
#include "IUDPSessionCallback.h"
#include "YMessageBlock.h"
namespace ysimple_network
{
	YProactorTokenOnSendTo::YProactorTokenOnSendTo(void)
		: m_pCallback(nullptr)
		, m_pSession(nullptr)
	{
	}


	YProactorTokenOnSendTo::~YProactorTokenOnSendTo(void)
	{
	}

	void YProactorTokenOnSendTo::beginInit(PACKET_SPTR packet, ISendToCallback& callback, IUDPSession& fromSession, const YEndPoint& toEndPoint )
	{
		resetOverlapped();


		m_pCallback = &callback;
		m_pSession = &fromSession;
		m_pSession->addRef();
		m_toEndPoint = toEndPoint;
		m_packet = PACKETBLOCK_SPTR(new YPacketBlock(packet));
		m_wsabuf.buf = packet->getrd();
		m_wsabuf.len = packet->written();

	}

	void YProactorTokenOnSendTo::endInit( bool success )
	{
		if (success == false)
		{
			m_pSession->release();
			delete this;
		}
	}

	bool YProactorTokenOnSendTo::onCompleted( bool completionStatus, LONG_PTR completionKey, DWORD dwBytesTransfered )
	{
		
		m_pCallback->onSent(m_toEndPoint, *m_pSession, completionStatus, dwBytesTransfered);

		m_pSession->release();
		delete this;
		return true;
	}

	WSABUF* YProactorTokenOnSendTo::getWSABuf()
	{
		return &m_wsabuf;
	}

	DWORD YProactorTokenOnSendTo::getWSABufCount()
	{
		return 1;
	}

}

